package simulador;

import Utils.Semente;
import eduni.simjava.Sim_entity;
import eduni.simjava.Sim_event;
import eduni.simjava.Sim_port;
import eduni.simjava.Sim_stat;
import eduni.simjava.Sim_system;
import eduni.simjava.distributions.ContinuousGenerator;
import eduni.simjava.distributions.Sim_negexp_obj;
import eduni.simjava.distributions.Sim_random_obj;

public class Gerenciador extends Sim_entity{
	private Sim_port entrada, saidaUp, saidaDown;
	private double atraso, probDown;
	private Sim_random_obj probDownUp;
	ContinuousGenerator geradorServicoGerenciador;
	

	public Gerenciador(String nome, double taxaServico, double probDown) {
		super(nome);
		geradorServicoGerenciador = new Sim_negexp_obj("geradorServicoGerenciador", taxaServico, Semente.gera());
		
		this.probDown = probDown;
		entrada = new Sim_port("entrada");
		saidaUp = new Sim_port("saidaUpload");
		saidaDown = new Sim_port("saidaDownload");
		add_port(entrada);
		add_port(saidaUp);
		add_port(saidaDown);
		probDownUp = new Sim_random_obj("DownUp");

		Sim_stat stat;
		stat = new Sim_stat();
		stat.add_measure(Sim_stat.ARRIVAL_RATE);
		stat.add_measure(Sim_stat.QUEUE_LENGTH);
		stat.add_measure(Sim_stat.RESIDENCE_TIME);
		stat.add_measure(Sim_stat.SERVICE_TIME);
		stat.add_measure(Sim_stat.THROUGHPUT);
		stat.add_measure(Sim_stat.UTILISATION);
		stat.add_measure(Sim_stat.WAITING_TIME);
		set_stat(stat);

	}

	public void body() {
		while (Sim_system.running()) {
			Sim_event e = new Sim_event();
			// Get the next event
			sim_get_next(e);
			// Process the event
			sim_process(geradorServicoGerenciador.sample());
			// The event has completed service
			sim_completed(e);
			if (probDownUp.sample() < probDown) {
				//80% chance
				//sim_trace(1, "### Trace do servidor de UPLOAD ###");
				sim_schedule(saidaDown, atraso, 1);
			} else {
				//20% chance
				//sim_trace(1, "### Trace do servidor de DOWNLOAD ###");
				sim_schedule(saidaUp, atraso, 2);
			}
		}
	}
}
